﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Shell32;
using System.Runtime.InteropServices;

namespace CAUNetLogin.Util.SystemLib.Tools
{
    public static class Widget
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private extern static int LoadString(IntPtr hInstance,//应用程序实例句柄
            UInt32 uID,//资源ID
            StringBuilder lpBuffer,//存放字符串的缓冲区
            int nBufferMax//缓冲区大小
            );

        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        private extern static IntPtr LoadLibrary(string lpFileName);
        [DllImport("kernel32.dll")]
        private extern static bool FreeLibrary(IntPtr lib);


        public static bool PinToTaskBar(bool isLock, string appPath)
        {
            Shell shell = new Shell();
            Folder folder = shell.NameSpace(Path.GetDirectoryName(appPath));
            FolderItem app = folder.ParseName(Path.GetFileName(appPath));

            StringBuilder sVerb = new StringBuilder(255);
            IntPtr handler = LoadLibrary("Shell32.dll");

            LoadString(handler, isLock ? 5386u : 5387u, sVerb, sVerb.Capacity + 1);
            FreeLibrary(handler);

            foreach (FolderItemVerb Fib in app.Verbs())
            {
                if (Fib.Name == sVerb.ToString())
                {
                    Fib.DoIt();
                    return true;
                }
            }

            return false;

        }

    }
}
